package com.antd.admin.server.controller;


import com.antd.admin.api.dto.MenuDTO;
import com.antd.admin.api.entity.SysMenu;
import com.antd.admin.server.service.ISysMenuService;
import com.antd.common.auth.util.AntdSecurityUtil;
import com.antd.common.core.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author 子丶沫
 * @since 2020-11-18
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单管理")
public class SysMenuController {
    @Autowired
    private ISysMenuService menuService;
    @ApiOperation(("获取菜单路由"))
    @GetMapping("/getRouters")
    public R<Object> getRouters(){
        String userId = AntdSecurityUtil.getUser().getUserId();
        return R.ok(menuService.getRouters(userId));
    }

    @GetMapping("/findPermission")
    public R<List<SysMenu>> findPermission(){
        String userId = AntdSecurityUtil.getUser().getUserId();
        return R.ok(menuService.findPermissionList(userId));
    }
    @GetMapping("/getMenuList")
    @ApiOperation("获取全部菜单")
    public R<Object> getMenuList(){
        return R.ok(menuService.getAllMenuList());
    }

    @PostMapping
    @PreAuthorize("hasAnyAuthority('sys:menu:save')")
    @ApiOperation("保存菜单")
    public R<Object> saveMenu(@RequestBody SysMenu menu){
        return R.ok(menuService.saveMenu(menu));
    }

    @PutMapping
    @PreAuthorize("hasAnyAuthority('sys:menu:update')")
    @ApiOperation("修改菜单")
    public R<Object> updateMenu(@RequestBody SysMenu menu){
        return R.ok(menuService.updateMenu(menu));
    }
    @GetMapping("searchMenu")
    public R<Object> searchMenu(String searchText){
        return R.ok(menuService.searchMenu(searchText));
    }
    @DeleteMapping
    @PreAuthorize("hasAnyAuthority('sys:menu:delete')")
    @ApiOperation("删除菜单")
    public R<Object> deleteMenu(String ids){
        List<String> menuIdList = Arrays.asList(ids.split(",")).stream().collect(Collectors.toList());
        return R.ok(menuService.removeByIds(menuIdList));
    }
}
